package models

import (
	"errors"
	"github.com/beego/beego/v2/client/orm"
)

type Gsparchives struct {
	Id                    int64
	DepartmentId          int64
	Products              *ErpProducts `orm:"rel(fk);null;on_delete(do_nothing)"`
	TestStandard          string       `json:"test_standard"`
	TestSituation         string       `json:"test_situation"`
	TestLable             string       `json:"test_lable"`
	AppearanceQuality     string       `json:"appearance_quality"`
	PackingSituation      string       `json:"packing_situation"`
	QualitySituation      string       `json:"quality_situation"`
	QualityStandardChange string       `json:"quality_standard_change"`
	CreatedAt             int64        `json:"created_at"`
	CreatedBy             string       `json:"created_by"`
	UpdatedAt             int64        `json:"updated_at"`
	UpdatedBy             string       `json:"updated_by"`
}

//查询
type GsparchivesQueryParam struct {
	BaseQueryParam
	DepartmentId int64 `json:"depid"`
	Typeid       int   `json:"typeid"`
}

func (a *Gsparchives) TableName() string {
	return GsparchivesTableName()
}

func GsparchivesTableName() string {
	return TableName("erp_gsparchives")
}

func GsparchivesPageList(params *GsparchivesQueryParam, retate ...interface{}) ([]*Gsparchives, int64) {

	ls := make([]*Gsparchives, 0)
	query := orm.NewOrm().QueryTable(GsparchivesTableName()).RelatedSel("Products")
	if len(retate) > 0 {
		query = query.RelatedSel(retate...)
	}

	if params.Typeid > 0 {
		query = query.Filter("Products__TypeId", params.Typeid)
	}

	query = query.Filter("department_id", params.DepartmentId)

	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}

//添加产品为批量处理
func GsparchivesAdd(base *Gsparchives, product_ids []int64) error {

	if len(product_ids) <= 0 {
		return errors.New("请选择需要建档的商品")
	}
	to,_ := orm.NewOrm().Begin()
	gsparchives := make([]*Gsparchives, 0)
	for _, v := range product_ids {
		base.Products = &ErpProducts{Id: v}
		gsparchives = append(gsparchives, base)
	}
	if _, err := to.InsertMulti(len(gsparchives), gsparchives); err != nil {
		to.Rollback()
		return err
	}
	to.Commit()
	return nil
}

func GsparchivesEdit(base *Gsparchives) error {

	if base.Id == 0 {
		return errors.New("参数错误")
	}
	if _, err := orm.NewOrm().Update(base); err != nil {
		return err
	}
	return nil
}

//获取已建档用商品
func GetExistProductId(depid int64) []interface{} {
	var list orm.ParamsList
	orm.NewOrm().Raw("SELECT products_id FROM "+GsparchivesTableName()+" WHERE department_id=?", depid).ValuesFlat(&list)
	return list
}

//获取单条档案信息
func GetGsparchivesById(id, depid int64) (*Gsparchives, error) {
	r := new(Gsparchives)
	err := orm.NewOrm().QueryTable(GsparchivesTableName()).RelatedSel("Products").Filter("DepartmentId", depid).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}
